*************************************************************************
*																		*
*			Campaign Panel Book Do-File Chapter 2 - Table 10			*
*																		*
**************************************************************************


fre ?_dcontact* //these are the variables to be used

*Drop aiding variables created for table 7
drop ?_av* ?_sd* ?_help*
				
estimates clear
foreach year in a b {
foreach partei in cdu spd fdp gru link {
foreach welle of numlist 2/6 {
 logit `year'_dcontact_`partei'`welle' b808.`year'_pidfull1  `year'_polint2 ///
	if !missing(`year'_dcontact_`partei'2, `year'_dcontact_`partei'3, ///
				`year'_dcontact_`partei'4, `year'_dcontact_`partei'5, ///
				`year'_dcontact_`partei'6) & ///
				`year'_pidfull1~=801 [pw=`year'_weight]
				
margins `year'_pidfull1, post atmeans

estimates store `year'_point`partei'`welle'


capture gen `year'_help`partei'`welle'1 = _b[1.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'4 = _b[4.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'5 = _b[5.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'6 = _b[6.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'7 = _b[7.`year'_pidfull1] if e(sample)
capture gen `year'_help`partei'`welle'808 = _b[808.`year'_pidfull1] if e(sample)

}
}
}

* Aggregation of wave means
foreach year in a b {
foreach partei in cdu spd fdp gru link {

gen `year'_av`partei'1 = (`year'_help`partei'21 + `year'_help`partei'31 + ///
	`year'_help`partei'41 + `year'_help`partei'51 + `year'_help`partei'61)/5
label var `year'_av`partei'1 "`partei'"
gen `year'_av`partei'4 = (`year'_help`partei'24 + `year'_help`partei'34 + ///
	`year'_help`partei'44 + `year'_help`partei'54 + `year'_help`partei'64)/5
label var `year'_av`partei'4 "`partei'"
gen `year'_av`partei'5 = (`year'_help`partei'25 + `year'_help`partei'35 ///
	+ `year'_help`partei'45 + `year'_help`partei'55 + `year'_help`partei'65)/5
label var `year'_av`partei'5 "`partei'"
gen `year'_av`partei'6 = (`year'_help`partei'26 + `year'_help`partei'36 + ///
	`year'_help`partei'46 + `year'_help`partei'56 + `year'_help`partei'66)/5
label var `year'_av`partei'6 "`partei'"
gen `year'_av`partei'7 = (`year'_help`partei'27 + `year'_help`partei'37 + ///
	`year'_help`partei'47 + `year'_help`partei'57 + `year'_help`partei'67)/5
label var `year'_av`partei'7 "`partei'"
gen `year'_av`partei'808 = (`year'_help`partei'2808 + ///
	`year'_help`partei'3808 + `year'_help`partei'4808 + `year'_help`partei'5808 ///
	+ `year'_help`partei'6808)/5
label var `year'_av`partei'808 "`partei'"
}
}

fre a_avcdu1 //spelled out party = party DV, number = party IV

* Standard deviation
foreach year in a b {
foreach partei in cdu spd fdp gru link {
foreach uv in 1 4 5 6 7 808 {

gen `year'_sd`partei'`uv' = ///
	sqrt((((`year'_help`partei'2`uv' - `year'_av`partei'`uv')^2)+( ///
		(`year'_help`partei'3`uv' - `year'_av`partei'`uv')^2)+ ///
		((`year'_help`partei'4`uv' - `year'_av`partei'`uv')^2) ///
		+((`year'_help`partei'5`uv' - `year'_av`partei'`uv')^2)+ ///
		((`year'_help`partei'6`uv' - `year'_av`partei'`uv')^2))/5)

					
}
}
}


* Table 10

estimates clear
foreach year in a b {
foreach partei in cdu spd fdp gru link {

mean `year'_av`partei'*
estimates store `year'_av`partei'

foreach uv in 1 4 5 6 7 808 {
sum `year'_sd`partei'`uv'
estadd scalar `year'_std`partei'`uv' = r(mean) , replace

}
}
}


esttab a_avcdu a_avspd a_avfdp a_avgru a_avlink ///
	using "Tables/nice table 10.rtf", replace ///
	b(2) not nonumb stats(a_stdcdu1 a_stdspd1 a_stdfdp1 a_stdgru1 a_stdlink1 ///
		a_stdcdu4 a_stdspd4 a_stdfdp4 a_stdgru4 a_stdlink4 ///
		a_stdcdu5 a_stdspd5 a_stdfdp5 a_stdgru5 a_stdlink5 ///
		a_stdcdu6 a_stdspd6 a_stdfdp6 a_stdgru6 a_stdlink6 ///
		a_stdcdu7 a_stdspd7 a_stdfdp7 a_stdgru7 a_stdlink7 ///
		a_stdcdu808 a_stdspd808 a_stdfdp808 a_stdgru808 a_stdlink808 N) 	
	
esttab b_avcdu b_avspd b_avfdp b_avgru b_avlink ///
	using "Tables/nice table 10.rtf", append ///
	b(2) not nonumb stats(b_stdcdu1 b_stdspd1 b_stdfdp1 b_stdgru1 b_stdlink1 ///
		b_stdcdu4 b_stdspd4 b_stdfdp4 b_stdgru4 b_stdlink4 ///
		b_stdcdu5 b_stdspd5 b_stdfdp5 b_stdgru5 b_stdlink5 ///
		b_stdcdu6 b_stdspd6 b_stdfdp6 b_stdgru6 b_stdlink6 ///
		b_stdcdu7 b_stdspd7 b_stdfdp7 b_stdgru7 b_stdlink7 ///
		b_stdcdu808 b_stdspd808 b_stdfdp808 b_stdgru808 b_stdlink808 N) 	


*Drop aiding variables
drop ?_av* ?_sd* ?_help*

save "${data}\data2009_2013 participants 7 waves.dta", replace

		